package com.app.qbake.module.mine.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.wind.base.BaseActivity;
import com.wind.base.request.BaseRequest;
import com.wind.base.utils.ActivityUtil;
import com.wind.base.utils.Navigator;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.mine.FeedbackRequest;
import com.wind.data.mine.FeedbackResponse;
import com.app.qbake.R;
import com.app.qbake.base.di.ProviderModule;
import com.app.qbake.module.mine.activity.presenter.FeedbackPresenter;
import com.app.qbake.module.mine.activity.usecase.FeedbackUsecase;
import com.app.qbake.module.mine.mvp.FeedbackView;
import com.wind.toastlib.ToastUtil;
import com.wind.view.ValidateEditText;

import java.io.Serializable;


@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.mine.mvp.FeedbackView",
                responseCanonicalName = "com.wind.data.mine.FeedbackResponse",
                requestCanonicalName = "com.wind.data.mine.FeedbackRequest"

        ),
        api = @Api(httpMethod = Api.HttpMethod.POST, url = "user/feedback")
)
public class FeedbackActivity extends BaseActivity implements FeedbackView
{

    public static void start(Context context,ExtraInfo info){
        Navigator.navigate(context,FeedbackActivity.class,info);
    }
    private ValidateEditText et;

    FeedbackPresenter presenter;
    private int mType;
    private ExtraInfo mExtraInfo;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mExtraInfo=Navigator.getSerializableExtra(this);
        mType=mExtraInfo.type;
        setContentView(R.layout.activity_feedback);

        ProviderModule module=new ProviderModule();
        presenter=new FeedbackPresenter(new FeedbackUsecase(module.provideRetrofit()));
        presenter.attachView(this);

        $(R.id.tv_commit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (validate()){
                    String content=getText(et);
                    showOpLoadingIndicator();
                    presenter.execute(buildRequest(content));
                }
            }



        });

        et=$(R.id.et_content);

    }
    private BaseRequest buildRequest(String content) {
        FeedbackRequest request=new FeedbackRequest();
        request.setContent(content);
        request.setType(mType);
        if (!TextUtils.isEmpty(mExtraInfo.toUserId)){
            request.setTo_userId(mExtraInfo.toUserId);
        }
        return request;
    }



    private boolean validate() {
        return et.validate("请输入反馈内容");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter!=null){
            presenter.detachView(false);
        }
    }

    @Override
    public void onSuccess(FeedbackResponse response) {
        hideOpLoadingIndicator();
        ToastUtil.showToast(getActivity(),"反馈成功");
        ActivityUtil.finish(getActivity());
    }

    @Override
    protected void setTitle() {
        if (mType==FeedbackRequest.TYPE_FEEDBACK) {
            mTitleBar.setTitle("意见反馈");
        }else if (mType==FeedbackRequest.TYPE_REPORT){
            mTitleBar.setTitle("举报");
        }
    }


    public static class ExtraInfo implements Serializable{
        public int type;
        public String toUserId;

    }
}
